package cn.uexpo.lhz.common.utils;


import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;



/**

 * Created by wangzhipeng on 2017/3/16.

 */

public class BeanCopyUtils {

    public static void Copy(Object source, Object dest) throws Exception {

        // 获取属性

        BeanInfo sourceBean = Introspector.getBeanInfo(source.getClass(),Object.class);

        PropertyDescriptor[] sourceProperty = sourceBean.getPropertyDescriptors();



        BeanInfo destBean = Introspector.getBeanInfo(dest.getClass(),Object.class);

        PropertyDescriptor[] destProperty = destBean.getPropertyDescriptors();



        try {

            for (int i = 0; i < sourceProperty.length; i++) {



                for (int j = 0; j < destProperty.length; j++) {



                    if (sourceProperty[i].getName().equals(destProperty[j].getName())  && sourceProperty[i].getPropertyType() == destProperty[j].getPropertyType()) {

                        // 调用source的getter方法和dest的setter方法

                        destProperty[j].getWriteMethod().invoke(dest,sourceProperty[i].getReadMethod().invoke(source));

                        break;

                    }

                }

            }

        } catch (Exception e) {

            throw new Exception("属性复制失败:" + e.getMessage());

        }

    }



//    public static void main(String[] args) throws Exception{
//
//        AppointmentOrderParam appointmentOrderParam = new AppointmentOrderParam();
//
//        appointmentOrderParam.setAppointmentTime("2017/05/04");
//
//        appointmentOrderParam.setConditionDescription("这个毛病已经由来已久了，以前没钱看，现在有钱了，想彻底根治");
//
//        appointmentOrderParam.setExpertId("myh111000");
//
//        appointmentOrderParam.setOtherOrderId("0001123ll33");
//
//        appointmentOrderParam.setSubscriberName("呼兰芬");
//
//        appointmentOrderParam.setSubscriberPhone("11097777");
//
//        appointmentOrderParam.setTheDisease("呼吸病");
//
//        appointmentOrderParam.setTimeSlot(1);
//
//        appointmentOrderParam.setTreatmentAddress("北京第三附属医院");
//
//        appointmentOrderParam.setTreatmentPrice(200);
//
//        appointmentOrderParam.setUserId("kkk888");
//
//        AppointmentOrder appointmentOrder = new AppointmentOrder();
//
//        CopyUtils.Copy(appointmentOrderParam,appointmentOrder);
//
//        System.out.println(appointmentOrder);
//
//    }

}


